package com.jiandaoshou;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XRefreshViewFooter;
import com.jiandaoshou.adapter.DingDanRecyclerAdapter;
import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.entry.DingDanDetailEntry;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.DividerItemDecoration;
import com.jiandaoshou.utils.JsonUtil;
import com.jiandaoshou.utils.TimeUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class JinRiDingDanAct extends BaseActivity{
    private RecyclerView mRecyclerView;
    private XRefreshView xRefreshView;
    private DingDanRecyclerAdapter adapter;
    private LinearLayoutManager LayoutManager;
    private List<DingDanDetailEntry> datas;
    private String title;
    private int type=1;
    private String starttime,endtime;
    private String shopID;
    private int page=1,count=10;

    private TextView tv_dingdan;
    @Override
    public boolean showBackLogo() {
        return true;
    }

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_jinri_dingdan;
    }

    @Override
    protected String getTitleText() {
        type=getIntent().getIntExtra("type",1);
        switch (type){
            case 1:
                title="本月订单";
                break;
            case 2:
                title="今日订单";
                break;
            case 3:
                title="昨日订单";
                break;
            case 4:
                title="前日订单";
                break;
        }
        return title;
    }

    @Override
    public void init() {
        //获取订单类型
        type=getIntent().getIntExtra("type",1);
        tv_dingdan = $(R.id.tv_dingdan);
        type=getIntent().getIntExtra("type",1);
        switch (type){
            case 1:
                tv_dingdan.setText("本月订单");
                break;
            case 2:
                tv_dingdan.setText("今日订单");
                break;
            case 3:
                tv_dingdan.setText("昨日订单");
                break;
            case 4:
                tv_dingdan.setText("前日订单");
                break;
        }


        //shopID获取shopID
        shopID=getIntent().getStringExtra("shopID");
        switch (type){
            case 1:
                starttime= TimeUtil.getInstance().GetCurrentMonthFirstDay();
                endtime=TimeUtil.getInstance().GetCurrentMonthLastDay();
                break;
            case 2:
                starttime= TimeUtil.getInstance().GetCurrentDayBeginTime();
                endtime=TimeUtil.getInstance().GetCurrentDayEndTime();
                break;
            case 3:
                starttime= TimeUtil.getInstance().getLastDayBeginTime(-1);
                endtime=TimeUtil.getInstance().getLastDayEndTime(-1);
                break;
            case 4:
                starttime= TimeUtil.getInstance().getLastDayBeginTime(-2);
                endtime=TimeUtil.getInstance().getLastDayEndTime(-2);
                break;
        }
        datas=new ArrayList<DingDanDetailEntry>();
        mRecyclerView = $(R.id.recyclerview);
        xRefreshView = $(R.id.list_refresh);
        xRefreshView.setMoveForHorizontal(true);
        xRefreshView.setAutoLoadMore(true);
        mRecyclerView.setHasFixedSize(true);
        //设置布局管理器
        LayoutManager = new LinearLayoutManager(this);
        LayoutManager.setOrientation(OrientationHelper.VERTICAL);
        mRecyclerView.setLayoutManager(LayoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
        //设置增加或删除条目的动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        GetData(page,0);
        adapter = new DingDanRecyclerAdapter(this, datas);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemListener(new DingDanRecyclerAdapter.OnItemListener() {
            @Override
            public void OnClickListener(int pos, View view) {
                Intent intent=new Intent(JinRiDingDanAct.this,JinRiDingDetailAct.class);
                intent.putExtra("OrderID",datas.get(pos).getStrOrderID());
                intent.putExtra("payMode",datas.get(pos).getStrPaymentMode());
                intent.putExtra("aount",datas.get(pos).getnTradingAmount());
                intent.putExtra("time",datas.get(pos).getDtTransactionTime());
                startActivity(intent);
            }
        });
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setAutoLoadMore(true);
        xRefreshView.setHideFooterWhenComplete(false);
        adapter.setCustomLoadMoreView(new XRefreshViewFooter(this));
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page++;
                GetData(page,2);
            }

            @Override
            public void onRefresh() {
                super.onRefresh();
                page=1;
                datas.clear();
                GetData(page,1);
            }
        });
    }
    //网络请求
    private void GetData(int page, final int type){
        //获取网络请求的数据
        HashMap<String, String> map = new HashMap<>();
        map.put("strEndTime",endtime);
        map.put("strBeginTime",starttime);
        map.put("strShopID",shopID);
        map.put("strPayMode","");
        map.put("order","desc");
        map.put("strShopSonName","");
        map.put("sort","dtTransactionTime");
        map.put("rows",count+"");
        map.put("page",page+"");
        Http.postLoading(page == 1,APIDefine.TF_Interface_GetOrderMsg, map, new Http.HttpCallBack() {
            @Override
            public void onHttpSuccess(String response){
                try {
                     JSONObject object=new JSONObject(response);
                     JSONArray array= (JSONArray) object.get("OrderList");
                     if(array.length()==0){
                         return;
                     }
                     DingDanDetailEntry entry;
                     for(int i=0;i<array.length();i++){
                         entry=JsonUtil.parser.fromJson(array.get(i).toString(),DingDanDetailEntry.class);
                         datas.add(entry);
                     }
                    adapter.notifyDataSetChanged();
                    if (array.length() < 10) {
                        xRefreshView.setLoadComplete(true);
                        xRefreshView.stopLoadMore();
                    }
                    if(type==1){
                        xRefreshView.stopRefresh();
                    }else{
                        xRefreshView.stopLoadMore();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
